package com.qf.ayc.orders.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.ayc.customers.entity.Customers;
import com.qf.ayc.customers.mapper.CustomersMapper;
import com.qf.ayc.orders.entity.Orders;
import com.qf.ayc.orders.mapper.OrdersMapper;
import com.qf.ayc.orders.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.ayc.user.entity.Users;
import com.qf.ayc.user.mapper.UsersMapper;
import com.qf.ayc.utils.ResultVo;
import com.qf.ayc.utils.WxPayUtils;
import com.qf.ayc.vo.WxpayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author BeautyFairy
 * @since 2024-02-20
 */
@Service
@Slf4j
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private CustomersMapper customersMapper;

    @Override//订单列表接口
    public List<Orders> List() {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        log.info("订单信息=", orders);
        return orders;
    }

    @Override//订单删除接口
    public void delete(Integer orderid) {
        ordersMapper.deleteById(orderid);
    }

    @Override//查询订单
    public List<Orders> select(Orders orders) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq(orders.getOrdernumber() != null, "ordernumber", orders.getOrdernumber());
        List<Orders> orders1 = ordersMapper.selectList(wrapper);
        return orders1;
    }

    @Override
    public List<Orders> getlist(Integer pageNum, Integer limit) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        Page<Orders> page = new Page<>(pageNum, limit);
        ordersMapper.selectPage(page, wrapper);
        List<Orders> records = page.getRecords();
        log.info("分页所有的订单信息为={}", records);
        return records;
    }

    @Override//订单添加，返回微信支付的url
    public ResultVo add(Orders orders) {
        int insert = ordersMapper.insert(orders);
        if (insert > 0) {
            WxpayVo wxpayVo = new WxpayVo();
            wxpayVo.setOrderId(orders.getOrdernumber());
            wxpayVo.setOrderInfo(orders.getOrderinfo());
            wxpayVo.setTotalFee(orders.getTotalfee());
            String payUrl = WxPayUtils.getPayUrl(wxpayVo);
            return ResultVo.ok("200", "订单添加成功", payUrl);
        } else {
            return ResultVo.notok("-1", "订单添加失败");
        }
    }

    @Override//订单状态接口
    public String sta(String ordernumber) {
        String s = WxPayUtils.queryOrderStatus(ordernumber);
        if (s.equals("SUCCESS")) {
            QueryWrapper<Orders> wrapper = new QueryWrapper<>();
            wrapper.eq(ordernumber != null, "ordernumber", ordernumber);
            Orders orders = ordersMapper.selectOne(wrapper);
            orders.setStatus(1);
            int i = ordersMapper.updateById(orders);
            if (i > 0) {
                return s;
            } else {
                return "支付成功，数据库状态未修改";
            }
        } else {
            return s;
        }
    }
}
